package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.article.IArticleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.StatisticsPageDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.DateUtils;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.service.StatisticsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : StatisticsServiceImpl  //类名
 * @Description : 图文统计  //描述
 * @Author : ZhongYu //作者
 * @Date: 2023/8/3  9:33
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private IArticleClient articleClient;

    /**
     * 图文统计
     * @param beginDate
     * @param endDate
     * @Params:
     * @Return
     */
    @Override
    public ResponseResult newsDimension(String beginDate, String endDate) {
        //参数校验
        if (StringUtils.isBlank(beginDate)||StringUtils.isBlank(endDate)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Date beginTime = DateUtils.stringToDate(beginDate);
        Date endTime = DateUtils.stringToDate(endDate);
        //获取用户
        WmUser user = WmThreadLocalUtil.getUser();
        // 图文发布量,点赞数量,收藏数量
        Map<String, Object> resultMap = new HashMap<>();
        ResponseResult responseResult = articleClient.statisticsNum(user.getId(), beginTime, endTime);
        if (responseResult.getCode().equals(200)) {
            String data = JSON.toJSONString(responseResult.getData());
            Map map = JSON.parseObject(data, Map.class);
            resultMap.put("likesNum", map.get("likes") == null ? 0 : map.get("likes"));
            resultMap.put("collectNum", map.get("collections") == null ? 0 : map.get("collections"));
            resultMap.put("publishNum", map.get("newsCount") == null ? 0 : map.get("newsCount"));
        }

        return ResponseResult.okResult(resultMap);
   }

    /**
     * 文章统计
     * @param page
     * @param size
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseResult newsPage(Integer page, Integer size, String beginDate, String endDate) {
        WmUser user = WmThreadLocalUtil.getUser();
        StatisticsPageDto statisticsPageDto = new StatisticsPageDto(page,size,beginDate,endDate,user.getId());
        return articleClient.newPage(statisticsPageDto);
    }

    /**
     * 详细信息
     * @param id
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseResult newsDetail(String id, String beginDate, String endDate) {
        // TODO 详细信息
        return null;
    }
}
